#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define vec vector<int>
#define y1 y_____hahahaha_____1

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;

int read(){int x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn;}

const int N = 6e3+10;

int mod;

int f[N][N<<1],n,bas = N;

void upd(int &x){x = x + (x >> 31 & mod);}

int main(){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in * 3; mod = in;
	f[0][bas] = 1;
	for(int i = 0;i < n;i++)
		for(int j = -i;j <= i;j++){
			upd(f[i + 1][j + 1 + bas] += f[i][j + bas] - mod);
			upd(f[i + 2][j - 1 + bas] += (ll) f[i][j + bas] * (i + 1) % mod - mod);
			upd(f[i + 3][j + bas] += (ll) f[i][j + bas] * (i + 1) % mod * (i + 2) % mod - mod);
		}
	int ans = 0; for(int i = 0;i <= n;i++) upd(ans += f[n][i+bas] - mod);
	printf("%d\n",ans);
	return 0;
}
